#include "Menu.h"
#include <iostream>
#include <fstream>
//additional includes



using namespace std;

Menu::Menu()
{
}

void Menu::display_main_menu() const 
{
	cout << "Here is the MAIN MENU \n";
	cout << "1. Application info\n";  //EL A3
    cout << "2. Read file\n";	//EL A3
    cout << "3. Show queries\n";	//EL A3
    cout << "4. Find queries\n";	//EL A3
    cout << "5. Update queries\n";	//EL A3
    cout << "6. Exit application\n";	//EL A3

}
void Menu::display_info() const
{
	cout << "Here is the Info requested \n";//display Project Part I, team name and team members
	cout << "Project Part I\n"<< "Maroon-123-1\n" << "Ryan Jungman\nRyan Finke\nEdwin Lunando\n";  //RNJ A2
}

void Menu::read()
{
	cout << "Read file name then read store data \n";
	string name, street, file; //RF A1
	int x, num, zip, acc;	//RF A1
	cout << "Enter the file to read customer data: \n";	//RF A1
	cin >> file; 	//RF A1
	ifstream ist(file.c_str());	//RF A1
	ist >> x;	//RF A1
	for(int i=0; i<x; ++i){	//RF A1
		ist>>acc>>name>>num>>street>>zip;	//RF A1
		Address a(num,street,zip);	//RF A1
		Customer c(acc,name,a);	//RF A1
		custs.push_back(c);	//RF A1
	}	//RF A1
	
	string filename;	//EL A3
	cout<<"Enter the file to read in category and product data: \n";	//RF
    cin >> filename;	//EL A3
    fstream f;	//EL A3
    f.open(filename.c_str());	//EL A3
    int n = 0;	//EL A3
    f >> n;	//EL A3
    for (int i = 0 ;i < n; i++) {	//EL A3
        int id = 0;	//EL A3
        string name = "";	//EL A3
        f >> id >> name;	//EL A3
        Category c(id,name);	//EL A3
        cats.push_back(c);	//EL A3
    }
    
	f >> n;	//RF
	for(int i = 0; i<n; ++i){	//RF
		int prod_id, cat_id;	//RF
		string prod_name;	//RF
		double prod_price;	//RF
		f >> prod_id>>cat_id>>prod_name>>prod_price;	//RF
		Product p(prod_id, cat_id, prod_name, prod_price);	//RF
		prods.push_back(p);	//RF
	}
	f.close(); //EL A3
	
	//string file;  										     //RNJ
	int cart, cust, y, m, d; 							 //RNJ
	cout << "Enter the file name to read in cart and cart item data : \n"; 		 //RNJ
	cin >> file; 											 //RNJ
	//fstream f;												 //RNJ
	f.open(file.c_str()); 									 //RNJ
	f >> x; 												 //RNJ
	for(int i=0; i<x; ++i) 									 //RNJ
	{ 														 //RNJ   reads in the carts
		f>>cart>>cust>>y>>m>>d; 							 //RNJ
		Date dte(y,m,d); 									 //RNJ
		Cart c(cart,cust,dte); 								 //RNJ
		carts.push_back(c); 								 //RNJ
	} 														 //RNJ
	f >> x; 												 //RNJ
	for(int i=0; i<x; ++i) 									 //RNJ
	{ 														 //RNJ
	int c_item,c_id,p_id,p_qty; 							 //RNJ     Reads in the cart items
		f>>c_item>>c_id>>p_id>>p_qty; 						 //RNJ
		Cart_item c(c_item,c_id,p_id,p_qty); 				 //RNJ
		cart_items.push_back(c); 							 //RNJ
	} 							 							 //RNJ
//	f.close;  											 //RNJ
	

}

bool myfunction(Customer a, Customer b){		//RF S2
	return (a.get_Name() < b.get_Name());		//RF S2
}												//RF S2

bool lowerID(Category a, Category b) 			//EL S1
{												//EL S1
	return (a.getID() < b.getID());				//EL S1
}

void Menu::show() const
{
	cout << "Show queries \n";
	vector<Customer> temp = custs;			//RF S2
	vector<Category> tempc = cats;			//EL S1
	sort(temp.begin(), temp.end(), myfunction);	//RF S2
	sort(tempc.begin(), tempc.end(), lowerID); //EL S1
	for(int i=0; i<temp.size(); ++i){			//RF S2
		cout<<temp[i].get_Name()<<" "<<temp[i].get_id()<<" "<<temp[i].get_Address().get_Num()<<" "	//RF S2
			<<temp[i].get_Address().get_Street()<<" "<<temp[i].get_Address().get_Zip()<<endl;		//RF S2
	}																								//RF S2
	
	for(int i=0; i<tempc.size(); ++i){																//EL S1
		cout<<tempc[i].getID() << " " << tempc[i].getName()<<endl;									//EL S1
	}																								//EL S1
	
		
}
void Menu::find() const 
{
	cout << "Find queries \n";
	cout << "Enter customer's name: ";					//RF S2 
	string name;										//RF S2 
	int customer_id, cart_id, prod_quantity, prod_id;	//RF S2 
	double prod_price;									//RF S2 
	cin>>name;											//RF S2 
	for(int i=0; i<custs.size(); ++i){					//RF S2 
		if(custs[i].get_Name() == name)					//RF S2 
			customer_id = custs[i].get_id();			//RF S2 
	}													//RF S2 
	for(int i=0; i<carts.size(); ++i){					//RF S2 
		if(customer_id == carts[i].get_cust_id())		//RF S2 
			cart_id = carts[i].get_cart_id();			//RF S2 
	}													//RF S2 
	for(int i=0; i<cart_items.size(); ++i){				//RF S2 
		if(cart_id == cart_items[i].get_cart_id()){		//RF S2 
			prod_quantity = cart_items[i].get_prod_qty();
			prod_id = cart_items[i].get_prod_id();		//RF S2 
		}												//RF S2 
	}													//RF S2 
	for(int i=0; i<prods.size(); ++i){					//RF S2 
		if(prod_id == prods[i].get_prod_id())			//RF S2 
			prod_price = prods[i].get_prod_price();		//RF S2 
	}													//RF S2 
	
	cout<<"Total sells of "<<name<<" are $"<<prod_price<<endl;	//RF S2
	
	printf("Enter category's name\n");					//EL S1
	string cat;											//EL S1
	int prod_q = 0, pro_id = 0;							//EL S1
	double pro_pri = 0.0;								//EL S1
	cin >>cat;											//EL S1
	for(int x = 0; x < prods.size(); x++) {				//EL S1
		if (prods[x].get_prod_name() == cat) {			//EL S1
			pro_id = prods[x].get_prod_id();			//EL S1
			pro_pri = prods[x].get_prod_price();		//EL S1
			break;										//EL S1
		}												//EL S1
	}													//EL S1
	
	for (int x = 0; x < cart_items.size(); x++) {		//EL S1
		if (cart_items[x].get_prod_id() == pro_id) {	//EL S1
			prod_q += cart_items[x].get_prod_qty();		//EL S1
		}
	}
	double total = prod_q * pro_pri;					//EL S1
	cout<<"Total sells of "<<cat<< " are $ "<< total<<endl;//EL S1
	
}
void Menu::update()  
{
	cout << "Update queries \n";
	cout << "Enter a new customer. Enter the customer id, cutomer name, Address number, Address street, and then zip code."<<endl;	//RF S2
	int cust_id, ad_num, zip;								//RF S2
	string name, ad_street;									//RF S2
	cin>>cust_id>>name>>ad_num>>ad_street>>zip;				//RF S2
	Address a(ad_num, ad_street, zip);						//RF S2
	Customer c(cust_id, name, a);							//RF S2
	custs.push_back(c);										//RF S2
	
	printf("Enter a new category. ID, name\n");				//EL S1
	int id;													//EL S1
	string namec;											//EL S1
	cin>>id>>namec;											//EL S1
	Category ca(id,namec);									//EL S1
	cats.push_back(ca);										//EL S1
	
}

